package com.yami.shop.multishop.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.MuseumModel;
import com.yami.shop.bean.model.SingleChoiceAnswer;
import com.yami.shop.bean.model.SingleChoiceQue;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.security.multishop.util.SecurityUtils;
import com.yami.shop.service.MuseumModelService;
import com.yami.shop.service.SingleChoiceAnswerService;
import com.yami.shop.service.SingleChoiceQueService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * 猜谜管理
 *
 * @author lsr
 */
@RestController
@RequestMapping("/shop/singleChoice")
@Tag(name="猜谜管理相关接口")
public class SingleChoiceController {

    @Autowired
    private SingleChoiceQueService singleChoiceQueService;
    @Autowired
    private SingleChoiceAnswerService singleChoiceAnswerService;

    @GetMapping("/page")
    public ServerResponseEntity<IPage<SingleChoiceQue>> page(PageParam<SingleChoiceQue> pageDTO, @ParameterObject SingleChoiceQue singleChoiceDTO) {
        QueryWrapper<SingleChoiceQue> museumQueryWrapper = new QueryWrapper<>();
        museumQueryWrapper.like(ObjectUtil.isNotEmpty(singleChoiceDTO.getName()),"name",singleChoiceDTO.getName());
        museumQueryWrapper.eq("create_at", SecurityUtils.getShopUser().getShopId());
        IPage<SingleChoiceQue> singleChoicePage = singleChoiceQueService.page(pageDTO,museumQueryWrapper);
        return ServerResponseEntity.success(singleChoicePage);
    }

    @GetMapping("/info/{singleChoiceId}")
    public ServerResponseEntity<SingleChoiceQue> getBysingleChoiceId(@PathVariable Long singleChoiceId) {
        SingleChoiceQue singleChoiceQue = singleChoiceQueService.getById(singleChoiceId);
        // 获取答案信息
        List<SingleChoiceAnswer> answers = singleChoiceAnswerService.list(new QueryWrapper<SingleChoiceAnswer>().eq("que_id", singleChoiceId));
        singleChoiceQue.setSingleChoiceAnswers(answers);
        return ServerResponseEntity.success(singleChoiceQue);
    }

    @PostMapping
    public ServerResponseEntity<Void> save(@Valid @RequestBody SingleChoiceQue singleChoice) {
        singleChoice.setCreateAt(SecurityUtils.getShopUser().getShopId());
        singleChoiceQueService.save(singleChoice);
        // 保存答案信息
        singleChoice.getSingleChoiceAnswers().forEach(s->{
            s.setQueId(singleChoice.getId());
        });
        singleChoiceAnswerService.saveBatch(singleChoice.getSingleChoiceAnswers());
        return ServerResponseEntity.success();
    }

    @PutMapping
    public ServerResponseEntity<Void> update(@Valid @RequestBody SingleChoiceQue singleChoice) {
        singleChoiceQueService.updateById(singleChoice);
        // 修改答案信息
        singleChoiceAnswerService.remove(new QueryWrapper<SingleChoiceAnswer>().eq("que_id",singleChoice.getId()));
        singleChoice.getSingleChoiceAnswers().forEach(s->{
            s.setQueId(singleChoice.getId());
        });
        singleChoiceAnswerService.saveBatch(singleChoice.getSingleChoiceAnswers());
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    public ServerResponseEntity<Void> delete(@RequestParam Long singleChoiceId) {
        singleChoiceQueService.removeById(singleChoiceId);
        return ServerResponseEntity.success();
    }

    @GetMapping("/list")
    public ServerResponseEntity<List<SingleChoiceQue>> getList(){
        List<SingleChoiceQue> ques = singleChoiceQueService.list(new QueryWrapper<SingleChoiceQue>().eq("create_at", SecurityUtils.getShopUser().getShopId()));
        return ServerResponseEntity.success(ques);
    }

}
